// pages/signIn/signIn.js
var app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    screenWidth: '',
    count: '1',
    showtab: false,
    loading: false
  },


  setTap: function(e) {
    var that = this
    //点击获取列表的序列号
    var id = e.currentTarget.dataset.index
    //根据序列号取出对应的日期，课程号，班级号等信息
    var date = this.data.list[id].date
    var course_num = this.data.list[id].course_num
    that.setData({
      course_num: course_num
    })
    var class_num = this.data.list[id].class_num
    wx.showActionSheet({
      itemList: ['开启签到摄像头', '查看签到记录', '手动登记签到'],
      success(res) {
        //获取点击对应的序列号
        var tapIndex = res.tapIndex
        switch (tapIndex) {
          //根据序列号执行对应的事件
          //index为0时，执行开启签到摄像头事件
          case 0:
            {
              var time = util.formatDate(new Date())
              var time = time.match(/(\d*-)*\d*/)[0]
              if (date != time) {
                wx.showModal({
                  title: '提示',
                  content: '无法开启非当天的课程签到',
                  showCancel: false
                })
              } else {
                that.setData({
                  show: true
                })
              }
              break;
            }
            //index为1时，执行查看签到记录事件
          case 1:
            {
              wx.navigateTo({
                url: '../signIn/signInRecord/signInRecord?date=' + date + '&course_num=' + course_num + '&class_num=' + class_num,
              })
              break;
            }

          case 2:
            {
              wx.navigateTo({
                url: '../signIn/signInRecord/signInRecord?date=' + date + '&course_num=' + course_num + '&class_num=' + class_num,
              })
              break;
            }
        }
      },
    })
  },

  lintap: function(e) {
    var that = this
    that.setData({
      count: e.detail
    })
  },

  confirmTap: function(e) {
    var that = this
    var course_num = that.data.course_num
    if (that.data.count.count != undefined) {
      var time = that.data.count.count
    } else {
      time = that.data.count
    }
    that.setData({
      show: false
    })
    that.openTV(time, course_num)
  },

  hidePopup: function() {
    var that = this
    that.setData({
      show: false
    })
  },

  openTV: function(time, course_num) {
    wx.request({
      url: 'http://113.78.182.235/open',
      method: 'GET',
      data: {
        time: time,
        att_course_num: course_num
      },
      success: function(res) {
        wx.showToast({
          title: '开启成功！',
          icon: 'success'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var identity = app.globalData.identity
    var header = app.globalData.header
    var num = wx.getStorageSync("username")
    this.setData({
      loading: true
    })
    if (identity === "student") {
      wx.request({
        url: 'http://139.199.192.171:8080/nanitao/stu/courselist.action',
        method: 'GET',
        header: header,
        data: {
          stunum: num
        },
        success: function(res) {
          that.setData({
            list: res.data,
            loading: false
          })
        }
      })
    } else if (identity === "teacher") {
        wx.request({
          url: 'http://139.199.192.171:8080/nanitao/tea/courselist.action',
          method: 'GET',
          header: header,
          data: {
            teanum: num
          },
          success: function (res) {
            that.setData({
              list: res.data,
              loading: false
            })
          }
        })
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          screenWidth: res.screenWidth
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})